package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.anno.AutoFill;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.enums.AutoFillType;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SetmealMapper {
    /**
     * 新增套餐
     * @param setmeal
     */
    @AutoFill(type=AutoFillType.INSERT)
    void insert(Setmeal setmeal);

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    Page<SetmealVO> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO);

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Select("select * from setmeal where id = #{id}")
    Setmeal getById(Long id);

    /**
     * 根据id删除套餐
     * @param setmealId
     */
    @Delete("delete from setmeal where id = #{id}")
    void deleteById(Long setmealId);

    /**
     * 根据id查询套餐和套餐菜品关系
     * @param id
     * @return
     */
    SetmealVO getByIdWithDish(Long id);

    @AutoFill(type=AutoFillType.UPDATE)
    int update(Setmeal setmeal);

    @Select("SELECT COUNT(*) FROM setmeal sm, setmeal_dish smd WHERE sm.id = smd.setmeal_id AND sm.status = #{status} AND smd.dish_id = #{dishId}")
    int countByStatusAndDishId(@Param("status") Integer status, @Param("dishId")Long dishId);

    /**
     * 不推荐！！！如果有动态标签，推荐写到xml中
     * */
    @Select("<script>" +
            "select * from setmeal where category_id = #{categoryId} <if test='status != null'> and status = #{status}</if> " +
            "</script>")
    List<Setmeal> selectByCategoryId(@Param("status") Integer status,@Param("categoryId") Long categoryId);

    @Select("SELECT smd.name, smd.copies, d.description, d.image " +
            "FROM setmeal_dish smd " +
            "LEFT JOIN dish d ON smd.dish_id = d.id " +
            "WHERE smd.setmeal_id = #{id}")
    List<DishItemVO> selectDishListById(Long id);
}
